/*
 * UsbStream.h
 *
 *  Created on: 2023年3月23日
 *  Author: 谢建明
 */

#ifndef TRUNK_SRC_USBAVSTREAM_H_
#define TRUNK_SRC_USBAVSTREAM_H_

#include <map>
#include <string>
#include <mutex>
#include "UsbStream.h"
#include "UacUtil.h"
#include "Poller/Timer.h"

namespace Media{
using namespace std;
using namespace Usbstream;





using callback_hotplug = function<bool(string usbport_key, bool status)>;

class UsbAVStream{

public:
    std::string USB_IN_1 =  "usb-1.1";
    std::string USB_IN_2 =  "usb-1.2";
    
public:
	static UsbAVStream *instance();
	bool addDevice(string usbport, int ability, int devicenum);
	bool removeDevice(string usbport, int ability);
	bool getAblilityMap(map<string, int> &abilitys);
	bool getAbility(string key, int &ability);


	bool getSupportParams(string usbport, int ability, Json::Value &params);

	bool setParams(string usbport, int ability, const Json::Value params);

	bool startStream(string usbport, int ability, callback_t cb);

	bool stopStream(string usbport, int ability);

	bool writeStream(string usbport, int ability, void *buffer, int length);

	bool addHotplugCb(string usbport, callback_hotplug cb);
	bool removeHotplugCb(string usbport);

	bool getDeviceName(string port, string &devName);

	bool notiftyHotplugIn1();

	bool notiftyHotplugIn2();

	bool getUsbStreamPos(map<string, string>& posInfo, bool& isUpdate);

	bool getUsbPlayPos(map<string, string>& posInfo);

	bool getUacOutStatus(string &usbPos);
private:
	bool addAbility(string usbport, int ability);
	bool removeAbility(string usbport, int ability);
	bool isVisibility(string usbport);
	//下面的两个接口直接使用的话会引起对象指针难以管理的问题，所以暂时私有化，不对外提供直接操作map的接口对应的功能放到上面五个接口来操作
	bool getDeviceMap(map<string, UsbStream *> &devices);
	bool getDevice(string key, UsbStream **device);
private:
	static UsbAVStream *sm_usbAVStream;
	static mutex m_instanceLock;
	map<string, UsbStream*> m_deviceMap;
	map<string, int> m_usbAbility;
	map<string, long long> m_usbTime;
	map<string, callback_hotplug> m_cbMap;
	map<string, int> m_videoDeviceNumberMap;
	mutex m_deviceLock;

	std::shared_ptr<Fdt::Timer>             m_hotplugTimer1;
	std::shared_ptr<Fdt::Timer>             m_hotplugTimer2;

	map<string, string>                     m_usbStreamPos = {{"video", "usb-1.2"}, {"audio", "usb-1.2"}};
	std::mutex                              m_posMutex;

	map<string, bool>                       m_cbMask;
};

}


#endif /* TRUNK_SRC_USBAVSTREAM_H_ */
